import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/index.vue'

import base from '@/utils/utils'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login/index.vue')
  },
  {
    path: '/:catchAll(.*)',
    name: '404',
    component: () => import(/* webpackChunkName: "404" */ '../views/errorPages/404.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 基本权限控制
const LOGIN_PAGE_NAME = 'Login'

// get token
const token = base.getSessionStorage('token')

router.beforeEach(async (to, from, next) => {
  if (!token && to.name !== LOGIN_PAGE_NAME) {
    next({
      name: LOGIN_PAGE_NAME
    })
  } else {
    next()
  }
})

// 测试

export default router
